<?php

declare(strict_types=1);

namespace App\Model\PssModel;

use App\Model\BaseModel\BasePssModel;
use App\Service\Code\Normal;

class MerchantBuyer extends BasePssModel
{
    public $table = 'merchant_buyer';

    public function getBuyerInfoList($param = [])
    {
        $body = $param['body'];
        $binfo = $param['binfo'];
        $page = isset($body['page']) ? $body['page'] : 1;
        $where = [];
        if (!empty($body['keyword'])) {
            $where[] = ['buyer_realname', 'like', '%' . $body['keyword'] . '%', 'OR'];
            $where[] = ['buyer_id_card', 'like', '%' . $body['keyword'] . '%', 'OR'];
        }
        $page_data = $this
            ->select(['id', 'buyer_realname', 'buyer_id_card', 'buyer_phone'])
            ->where('bid', $binfo['bid'])
            ->where(function ($query) use ($where) {
                $query->orWhere($where);
            })->paginate(10, ['*'], 'page', $page, false)->toArray();
        if (isset($body['is_cut_id_card'])) {
            $data = [];
            foreach ($page_data['data'] as &$item) {
                $item['buyer_id_card'] = $this->cutIdCard($item['buyer_id_card']);
                $data[] = $item;
            }
            $page_data['data'] = $data;
        }
        return Normal::returnCode(Normal::SUC, "获取成功", $page_data);
    }

    /**
     * 自动添加订购人信息
     * @param array $param
     * @return mixed
     */
    public function checkAddUserBuyerInfo($binfo = [], $param = [])
    {
        try {

            if (empty($binfo['bid'])) {
                return Normal::returnCode(Normal::FAIL, "缺少bid");
            }

            if (empty($param['buyer_realname'])) {
                return Normal::returnCode(Normal::FAIL, "缺少buyer_realname");
            }

            if (empty($param['buyer_phone'])) {
                return Normal::returnCode(Normal::FAIL, "缺少buyer_phone");
            }

            if (empty($param['buyer_id_card'])) {
                return Normal::returnCode(Normal::FAIL, "缺少buyer_id_card");
            }

            $info = $this->info([
                'bid' => $binfo['bid'],
                'buyer_realname' => $param['buyer_realname'],
                'buyer_id_card' => $param['buyer_id_card'],
                'buyer_phone' => $param['buyer_phone'],
            ]);

            if (!empty($info)) {
                return Normal::returnCode(Normal::FAIL, "订购人信息已存在");
            }

            $insert = [
                'bid' => $binfo['bid'],
                'buyer_realname' => $param['buyer_realname'],
                'buyer_id_card' => $param['buyer_id_card'],
                'buyer_phone' => $param['buyer_phone'],
                'cd' => time(),
            ];

            $this->add($insert);
            return Normal::returnCode(Normal::SUC, "订购人信息写入成功");
        } catch (\Throwable $t) {
            return Normal::returnCode(Normal::SUC, $t->getMessage());
        }
    }

    /**
     * 自动添加订购人信息
     * @param array $param
     * @return mixed
     */
    public function checkUpdateUserBuyerInfo($binfo = [], $param = [])
    {
        try {

            if (empty($binfo['bid'])) {
                return Normal::returnCode(Normal::FAIL, "缺少bid");
            }

            if (empty($param['id'])) {
                return Normal::returnCode(Normal::FAIL, "缺少id");
            }

            if (empty($param['buyer_realname'])) {
                return Normal::returnCode(Normal::FAIL, "缺少buyer_realname");
            }

            if (empty($param['buyer_phone'])) {
                return Normal::returnCode(Normal::FAIL, "缺少buyer_phone");
            }

            if (empty($param['buyer_id_card'])) {
                return Normal::returnCode(Normal::FAIL, "缺少buyer_id_card");
            }

            $info = $this->info([
                'bid' => $binfo['bid'],
                'buyer_realname' => $param['buyer_realname'],
                'buyer_id_card' => $param['buyer_id_card'],
                'buyer_phone' => $param['buyer_phone'],
                ['id', '<>', $param['id']]
            ]);

            if (!empty($info)) {
                return Normal::returnCode(Normal::FAIL, "订购人信息已存在");
            }

            $update = [
                'bid' => $binfo['bid'],
                'buyer_realname' => $param['buyer_realname'],
                'buyer_id_card' => $param['buyer_id_card'],
                'buyer_phone' => $param['buyer_phone'],
                'cd' => time(),
            ];

            $this->where('id', $param['id'])->update($update);
            return Normal::returnCode(Normal::SUC, "订购人信息编辑成功");
        } catch (\Throwable $t) {
            return Normal::returnCode(Normal::SUC, $t->getMessage());
        }
    }

    /**
     * 删除订购人信息
     * @param array $param
     */
    public function checkDelUserBuyerInfo($binfo = [], $param = [])
    {
        try {

            if (empty($binfo['bid'])) {
                return Normal::returnCode(Normal::FAIL, "缺少bid");
            }

            if (empty($param['id'])) {
                return Normal::returnCode(Normal::FAIL, "缺少id");
            }

            $info = $this->info([
                'bid' => $binfo['bid'],
                'id' => $param['id']
            ]);

            if (empty($info)) {
                return Normal::returnCode(Normal::FAIL, "订购人信息不存在");
            }

            $this->where('id', $param['id'])->delete();
            return Normal::returnCode(Normal::SUC, "订购人信息删除成功");
        } catch (\Throwable $t) {
            return Normal::returnCode(Normal::SUC, $t->getMessage());
        }
    }

    /**
     * 截取身份证
     * @param $idCard
     * @return string
     */
    private function cutIdCard($idCard)
    {
        return substr($idCard, 0, 6) . "****" . substr($idCard,-4);
    }
}